package com.hm.shopping.auto;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.hm.base.service.SubscriberService;
import com.hm.base.vo.SubscriberVo;
import com.hm.common.util.ChineseToPinYinUtil;
import com.hm.shopping.def.AdvertisingLocationEnum;
import com.hm.shopping.def.NewsAnnouncementEnum;
import com.hm.shopping.service.AdvertisingService;
import com.hm.shopping.service.ClassificationService;
import com.hm.shopping.service.GoodsIntroductionService;
import com.hm.shopping.service.NewsAnnouncementService;
import com.hm.shopping.service.UserService;
import com.hm.shopping.vo.AdvertisingVo;
import com.hm.shopping.vo.ClassificationDetailVo;
import com.hm.shopping.vo.ClassificationVo;
import com.hm.shopping.vo.GoodsIntroductionVo;
import com.hm.shopping.vo.NewsAnnouncementVo;
import com.hm.shopping.vo.UserVo;
import com.hm.shopping.vo.su.GoodsAttributeVo;
import com.hm.shopping.vo.su.GoodsAttributeVo.GoodsAttributesInfoVo;
import com.hm.shopping.vo.su.GoodsParameterVo;
import com.hm.shopping.vo.su.UserAddressVo;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年10月21日 下午11:55:03
 * @version 1.0
 * @describe
 */
@Slf4j
@Component
@SuppressWarnings("unused")
public class HmApplicationListenerStart implements ApplicationListener<ContextRefreshedEvent> {

	@Autowired
	private AdvertisingService advertisingService;

	@Autowired
	private ClassificationService classificationService;

	@Autowired
	private NewsAnnouncementService newsAnnouncementService;

	@Autowired
	private GoodsIntroductionService goodsIntroductionService;

	@Autowired
	private UserService userService;

	@Autowired
	private SubscriberService subscriberService;

	public void onApplicationEvent(ContextRefreshedEvent event) {
		log.info("系统初始化数据");
	}
	
	/**
	 * 重置分类明细
	 */
	private void restClassificationDetail() {
		ClassificationDetailVo classificationDetailVo = new ClassificationDetailVo();
		{
			classificationDetailVo.setId("5a618f0b9bd8d560d074a6b5");
			classificationDetailVo.setClassificationDetailName("蒸蛋糕");
			classificationDetailVo.setClassificationDetailCode(ChineseToPinYinUtil.getPinYinHeadChar("蒸蛋糕".replace("/", "_or_")).toUpperCase());
			classificationDetailVo.setClassificationDetailIcon("http://p3cxjq83j.bkt.clouddn.com/pic_not_found.png");
			classificationDetailVo.setClassificationDetailOrder(1);
			
			List<GoodsAttributeVo> attributes = new ArrayList<GoodsAttributeVo>();
			{//商品属性
				GoodsAttributeVo vo = new GoodsAttributeVo();
				{
					vo.setAttributeType("来源");
					vo.setAttributeCode(ChineseToPinYinUtil.getPinYinHeadChar(vo.getAttributeType().replace("/", "_or_")).toUpperCase());
					
					List<GoodsAttributesInfoVo> attributeDetails = new ArrayList<GoodsAttributesInfoVo>();
					{
						GoodsAttributesInfoVo detailVo = new GoodsAttributesInfoVo();
						{
							detailVo.setName("日本");
							detailVo.setCode(ChineseToPinYinUtil.getPinYinHeadChar(detailVo.getName().replace("/", "_or_")).toUpperCase());
						}
						attributeDetails.add(detailVo);
						
						detailVo = new GoodsAttributesInfoVo();
						{
							detailVo.setName("意大利");
							detailVo.setCode(ChineseToPinYinUtil.getPinYinHeadChar(detailVo.getName().replace("/", "_or_")).toUpperCase());
						}
						attributeDetails.add(detailVo);
						
						detailVo = new GoodsAttributesInfoVo();
						{
							detailVo.setName("巴西");
							detailVo.setCode(ChineseToPinYinUtil.getPinYinHeadChar(detailVo.getName().replace("/", "_or_")).toUpperCase());
						}
						attributeDetails.add(detailVo);
					}
					vo.setAttributes(attributeDetails);
				}
				attributes.add(vo);
				
				vo = new GoodsAttributeVo();
				{
					vo.setAttributeType("口味");
					vo.setAttributeCode(ChineseToPinYinUtil.getPinYinHeadChar(vo.getAttributeType().replace("/", "_or_")).toUpperCase());
					
					List<GoodsAttributesInfoVo> attributeDetails = new ArrayList<GoodsAttributesInfoVo>();
					{
						GoodsAttributesInfoVo detailVo = new GoodsAttributesInfoVo();
						{
							detailVo.setName("甜");
							detailVo.setCode(ChineseToPinYinUtil.getPinYinHeadChar(detailVo.getName().replace("/", "_or_")).toUpperCase());
						}
						attributeDetails.add(detailVo);
						
						detailVo = new GoodsAttributesInfoVo();
						{
							detailVo.setName("咸");
							detailVo.setCode(ChineseToPinYinUtil.getPinYinHeadChar(detailVo.getName().replace("/", "_or_")).toUpperCase());
						}
						attributeDetails.add(detailVo);
					}
					vo.setAttributes(attributeDetails);
				}
				attributes.add(vo);
			}
			
			classificationDetailVo.setAttributes(attributes);
		}
		classificationService.updateClassificationDetail(classificationDetailVo);

	}

	/**
	 * 初始化用户数据
	 */
	private void initUser() {
		UserVo userVo = new UserVo();
		{
			SubscriberVo subscriberVo = subscriberService.getSubscriber(1l);
			userVo.setSubscriberId(subscriberVo.getId());

			List<UserAddressVo> userAddress = new ArrayList<UserAddressVo>();
			{
				UserAddressVo userAddressVo = new UserAddressVo();
				{
					userAddressVo.setTakeUserName("王林");
					userAddressVo.setTakeUserPhone("15680249548");
					userAddressVo.setDetailedAddress("四川省成都市金牛区XXxi小区5-12");
					userAddressVo.setAddressId(UUID.randomUUID().toString());
					userAddressVo.setRecentlyUse(true);
				}
				userAddress.add(userAddressVo);

				userAddressVo = new UserAddressVo();
				{
					userAddressVo.setTakeUserName("唐老鸭");
					userAddressVo.setTakeUserPhone("13280249548");
					userAddressVo.setDetailedAddress("四川省成都市武侯区,武侯立交外侧大柳树旁边有狗叫那家");
					userAddressVo.setAddressId(UUID.randomUUID().toString());
				}
				userAddress.add(userAddressVo);
			}
			userVo.setUserAddress(userAddress);
		}
		userService.refreshUser(userVo);
	}

	/**
	 * 初始化商品介绍信息
	 */
	private void initGoodsIntroduction() {
		GoodsIntroductionVo goodsIntroductionVo = new GoodsIntroductionVo();
		{
			goodsIntroductionVo.setShopsId("1");
			goodsIntroductionVo.setClassificationCodes(Arrays.asList("ZDG"));// 蒸蛋糕
			goodsIntroductionVo.setGoodsName("良品铺子 手剥松子218g 坚果炒货 巴西松子");
			goodsIntroductionVo.setUnitPrice(5690);
			goodsIntroductionVo.setInventory(890);
			goodsIntroductionVo.setSalesAmount(150);
			goodsIntroductionVo.setRemainingInventory(goodsIntroductionVo.getInventory());
			goodsIntroductionVo.setBanner("http://p2s9wr3uo.bkt.clouddn.com/imgsearch1.jpg");
			goodsIntroductionVo.setGoodsIntroductionPics(Arrays.asList("http://p2s9wr3uo.bkt.clouddn.com/01.jpg", "http://p2s9wr3uo.bkt.clouddn.com/02.jpg", "http://p2s9wr3uo.bkt.clouddn.com/03.jpg"));

			List<GoodsAttributeVo> goodsAttributes = new ArrayList<GoodsAttributeVo>();
			{
				GoodsAttributeVo goodsAttributeVo = new GoodsAttributeVo();
				{
					goodsAttributeVo.setAttributeType("口味");
					goodsAttributeVo.setAttributeCode(ChineseToPinYinUtil.getPingYin(goodsAttributeVo.getAttributeType()));
					List<GoodsAttributesInfoVo> attributes = new ArrayList<GoodsAttributesInfoVo>();
					{
						GoodsAttributesInfoVo attributesInfoVo = new GoodsAttributesInfoVo();
						{
							attributesInfoVo.setName("原味奶油");
							attributesInfoVo.setCode(ChineseToPinYinUtil.getPingYin(attributesInfoVo.getName()));
							attributesInfoVo.setDefaultSelected(true);
						}
						attributes.add(attributesInfoVo);

						attributesInfoVo = new GoodsAttributesInfoVo();
						{
							attributesInfoVo.setName("炭烧咸香");
							attributesInfoVo.setCode(ChineseToPinYinUtil.getPingYin(attributesInfoVo.getName()));
						}
						attributes.add(attributesInfoVo);
					}
					goodsAttributeVo.setAttributes(attributes);
				}
				goodsAttributes.add(goodsAttributeVo);

				// --------------------------------

				goodsAttributeVo = new GoodsAttributeVo();
				{
					goodsAttributeVo.setAttributeType("包装");
					goodsAttributeVo.setAttributeCode(ChineseToPinYinUtil.getPingYin(goodsAttributeVo.getAttributeType()));
					List<GoodsAttributesInfoVo> attributes = new ArrayList<GoodsAttributesInfoVo>();
					{
						GoodsAttributesInfoVo attributesInfoVo = new GoodsAttributesInfoVo();
						{
							attributesInfoVo.setName("手袋单人份礼盒");
							attributesInfoVo.setCode(ChineseToPinYinUtil.getPingYin(attributesInfoVo.getName()));
							attributesInfoVo.setDefaultSelected(true);
						}
						attributes.add(attributesInfoVo);

						attributesInfoVo = new GoodsAttributesInfoVo();
						{
							attributesInfoVo.setName("双人份全家福礼包");
							attributesInfoVo.setCode(ChineseToPinYinUtil.getPingYin(attributesInfoVo.getName()));
						}
						attributes.add(attributesInfoVo);
					}
					goodsAttributeVo.setAttributes(attributes);
				}
				goodsAttributes.add(goodsAttributeVo);
			}
			goodsIntroductionVo.setGoodsAttributes(goodsAttributes);

			List<GoodsParameterVo> goodsParameters = new ArrayList<GoodsParameterVo>();
			{
				goodsParameters.add(new GoodsParameterVo("产品类型", "烘炒类"));
				goodsParameters.add(new GoodsParameterVo("原料产地", "巴基斯坦"));
				goodsParameters.add(new GoodsParameterVo("产地", "湖北省武汉市"));
				goodsParameters.add(new GoodsParameterVo("配料表", "进口松子、食用盐"));
				goodsParameters.add(new GoodsParameterVo("产品规格", "210g"));
				goodsParameters.add(new GoodsParameterVo("保质期", "180天"));
				goodsParameters.add(new GoodsParameterVo("产品标准号", "GB/T 22165"));
				goodsParameters.add(new GoodsParameterVo("生产许可证编号", "QS4201 1801 0226"));
				goodsParameters.add(new GoodsParameterVo("储存方法", "请放置于常温、阴凉、通风、干燥处保存 "));
				goodsParameters.add(new GoodsParameterVo("食用方法", "开袋去壳即食"));
			}
			goodsIntroductionVo.setGoodsParameters(goodsParameters);
			goodsIntroductionVo.setContentDetails("<span>hello</span>");
		}

		goodsIntroductionService.addGoodsIntroduction(goodsIntroductionVo);
	}

	/**
	 * 初始化首页广告轮播
	 */
	private void initAdvertising() {
		Map<String, String> content = new HashMap<String, String>();
		{
			content.put("首页广告1", "http://p2s9wr3uo.bkt.clouddn.com/ad1.jpg");
			content.put("首页广告2", "http://p2s9wr3uo.bkt.clouddn.com/ad2.jpg");
			content.put("首页广告3", "http://p2s9wr3uo.bkt.clouddn.com/ad3.jpg");
			content.put("首页广告4", "http://p2s9wr3uo.bkt.clouddn.com/ad4.jpg");
		}

		content.forEach((key, value) -> {
			AdvertisingVo advertisingVo = new AdvertisingVo();
			{
				advertisingVo.setAdvertisingLocation(AdvertisingLocationEnum.HOME_PAGE_SHUFFLING);
				advertisingVo.setAdvertisingTitle(key);
				advertisingVo.setBanner(value);
				advertisingVo.setLinkUri("http://www.baidu.com");
			}
			advertisingService.addAdvertising(advertisingVo);
		});
	}

	/**
	 * 初始化分类明细
	 */
	private void initClassificationDetail() {
		ClassificationDetailVo classificationDetailVo = new ClassificationDetailVo();
		{
			classificationDetailVo.setClassificationDetailName("蒸蛋糕");
			classificationDetailVo.setClassificationDetailCode(ChineseToPinYinUtil.getPinYinHeadChar("蒸蛋糕".replace("/", "_or_")).toUpperCase());
			classificationDetailVo.setClassificationDetailOrder(1);
		}
		classificationService.addClassificationDetail(classificationDetailVo);

		initClassifications(Arrays.asList("5a618f0b9bd8d560d074a6b5"));// 分类id
	}

	/**
	 * 初始化分类
	 */
	private void initClassifications(List<String> classificationDetailIds) {
		Map<String, String> content = new LinkedHashMap<String, String>();
		{
			content.put("http://p2s9wr3uo.bkt.clouddn.com/cake.png", "点心/蛋糕");
			content.put("http://p2s9wr3uo.bkt.clouddn.com/cookies.png", "饼干/膨化");
			content.put("http://p2s9wr3uo.bkt.clouddn.com/meat.png", "熟食/肉类");
			content.put("http://p2s9wr3uo.bkt.clouddn.com/bamboo.png", "素食/卤味");
			content.put("http://p2s9wr3uo.bkt.clouddn.com/nut.png", "坚果/炒货");
			content.put("http://p2s9wr3uo.bkt.clouddn.com/candy.png", "糖果/蜜饯");
			content.put("http://p2s9wr3uo.bkt.clouddn.com/chocolate.png", "巧克力");
			content.put("http://p2s9wr3uo.bkt.clouddn.com/fish.png", "海味/河鲜");
			content.put("http://p2s9wr3uo.bkt.clouddn.com/tea.png", "花茶/果茶");
			content.put("http://p2s9wr3uo.bkt.clouddn.com/package.png", "品牌/礼包");
		}

		AtomicInteger index = new AtomicInteger(1);
		content.forEach((key, value) -> {
			ClassificationVo classificationVo = new ClassificationVo();
			{
				classificationVo.setClassificationName(value);
				classificationVo.setClassificationCode(ChineseToPinYinUtil.getPinYinHeadChar(value.replace("/", "_or_")).toUpperCase());
				classificationVo.setClassificationIcon(key);
				classificationVo.setClassificationDetailIds(classificationDetailIds);
				classificationVo.setClassificationOrder(index.getAndIncrement());
			}
			classificationService.addClassification(classificationVo);
		});
	}

	/**
	 * 初始化新闻公告
	 */
	private void initNewsAnnouncement() {
		AtomicInteger index = new AtomicInteger(1);

		NewsAnnouncementVo newsAnnouncementVo = new NewsAnnouncementVo();
		{
			newsAnnouncementVo.setNewsAnnouncement(NewsAnnouncementEnum.PREFERENTIAL);
			newsAnnouncementVo.setNewsName("商城爆品1分秒");
			newsAnnouncementVo.setContent("<span style='color:red'>新年快乐</span>");
			newsAnnouncementVo.setHeadlines(true);
			newsAnnouncementVo.setNewsOrder(index.incrementAndGet());
			newsAnnouncementService.addNewsAnnouncement(newsAnnouncementVo);
		}

		{
			newsAnnouncementVo.setNewsAnnouncement(NewsAnnouncementEnum.ANNOUNCEMENT);
			newsAnnouncementVo.setNewsName("商城与广州市签署战略合");
			newsAnnouncementVo.setContent("<span style='color:red'>国庆快乐</span>");
			newsAnnouncementVo.setHeadlines(true);
			newsAnnouncementVo.setNewsOrder(index.incrementAndGet());
			newsAnnouncementService.addNewsAnnouncement(newsAnnouncementVo);
		}

		// ------------------------------------

		{
			newsAnnouncementVo.setNewsAnnouncement(NewsAnnouncementEnum.PREFERENTIAL);
			newsAnnouncementVo.setNewsName("洋河年末大促，低至两件");
			newsAnnouncementVo.setContent("<span style='color:red'>元旦快乐</span>");
			newsAnnouncementVo.setHeadlines(false);
			newsAnnouncementVo.setNewsOrder(index.incrementAndGet());
			newsAnnouncementService.addNewsAnnouncement(newsAnnouncementVo);
		}

		{
			newsAnnouncementVo.setNewsAnnouncement(NewsAnnouncementEnum.ANNOUNCEMENT);
			newsAnnouncementVo.setNewsName("华北、华中部分地区配送");
			newsAnnouncementVo.setContent("<span style='color:red'>清明快乐</span>");
			newsAnnouncementVo.setHeadlines(false);
			newsAnnouncementVo.setNewsOrder(index.incrementAndGet());
			newsAnnouncementService.addNewsAnnouncement(newsAnnouncementVo);
		}

		{
			newsAnnouncementVo.setNewsAnnouncement(NewsAnnouncementEnum.PREFERENTIAL);
			newsAnnouncementVo.setNewsName("家电狂欢千亿礼券 买1送");
			newsAnnouncementVo.setContent("<span style='color:red'>大家一起快乐</span>");
			newsAnnouncementVo.setHeadlines(false);
			newsAnnouncementVo.setNewsOrder(index.incrementAndGet());
			newsAnnouncementService.addNewsAnnouncement(newsAnnouncementVo);
		}
	}
}
